package
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	public class InputManager
	{
		private var m_inputHandlers:Vector.<IInputHandler> = new Vector.<IInputHandler>;
		
		public function InputManager(stage:Stage, defaultInputHandler:IInputHandler)
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			m_inputHandlers.push(defaultInputHandler);
		}
		
		private function onMouseWheel(me:MouseEvent):void{
			m_inputHandlers[0].onMouseWheel(me);
		}
		
		public function setInputHandler(inputHandler:IInputHandler):void{
			m_inputHandlers.unshift(inputHandler);
		}
		
		public function removeInputHandler(inputHandler:IInputHandler):void{
			m_inputHandlers.shift();
		}
		
		private function onMouseMove(me:MouseEvent):void{
			m_inputHandlers[0].onMouseMove(me);
		}
		
		private function onMouseDown(me:MouseEvent):void{
			m_inputHandlers[0].onMouseDown(me);
		}
		
		private function onMouseUp(me:MouseEvent):void{
			m_inputHandlers[0].onMouseUp(me);
		}
		
		private function onKeyDown(ke:KeyboardEvent):void{
			m_inputHandlers[0].onKeyDown(ke);
		}
		
		private function onKeyUp(ke:KeyboardEvent):void{
			m_inputHandlers[0].onKeyUp(ke);
		}		
	}
}